home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / drivers1.zip / GETNUM.ASM < prev    next >
Assembly Source File  |  1992-01-17  |  2KB  |  78 lines

  1. ;put into the public domain by Russell Nelson, nelson@crynwr.com
  2.  
  3.     public    get_number
  4. get_number:
  5.     mov    bp,10            ;we default to 10.
  6.     jmp    short get_number_0
  7.  
  8.     public    get_hex
  9. get_hex:
  10.     mov    bp,16
  11. ;get a hex number, skipping leading blanks.
  12. ;enter with si->string of digits,
  13. ;    di -> dword to store the number in.  [di] is not modified if no
  14. ;        digits are given, so it acts as the default.
  15. ;return cy if there are no digits at all.
  16. ;return nc, bx:cx = number, and store bx:cx at [di].
  17. get_number_0:
  18.     call    skip_blanks
  19.     call    get_digit        ;is there really a number here?
  20.     jc    get_number_3
  21.     xor    ah,ah
  22.     cmp    ax,bp            ;larger than our base?
  23.     jae    get_number_3        ;yes.
  24.     or    al,al            ;Does the number begin with zero?
  25.     jne    get_number_4        ;no.
  26.     mov    bp,8            ;yes - they want octal.
  27. get_number_4:
  28.  
  29.     xor    cx,cx            ;get a hex number.
  30.     xor    bx,bx
  31. get_number_1:
  32.     lodsb
  33.     cmp    al,'x'            ;did they really want hex?
  34.     je    get_number_5        ;yes.
  35.     cmp    al,'X'            ;did they really want hex?
  36.     je    get_number_5        ;yes.
  37.     call    get_digit        ;convert a character into an int.
  38.     jc    get_number_2        ;not a digit (neither hex nor dec).
  39.     xor    ah,ah
  40.     cmp    ax,bp            ;larger than our base?
  41.     jae    get_number_2        ;yes.
  42.  
  43.     push    ax            ;save the new digit.
  44.  
  45.     mov    ax,bp            ;multiply the low word by ten.
  46.     mul    cx
  47.     mov    cx,ax            ;keep the low word.
  48.     push    dx            ;save the high word for later.
  49.     mov    ax,bp
  50.     mul    bx
  51.     mov    bx,ax            ;we keep only the low word (which is our high word)
  52.     pop    dx
  53.     add    bx,dx            ;add the high result from earlier.
  54.  
  55.     pop    ax            ;get the new digit back.
  56.     add    cx,ax            ;add the new digit in.
  57.     adc    bx,0
  58.     jmp    get_number_1
  59. get_number_5:
  60.     mov    bp,16            ;change the base to hex.
  61.     jmp    get_number_1
  62. get_number_2:
  63.     dec    si
  64.     mov    [di],cx            ;store the parsed number.
  65.     mov    [di+2],bx
  66.     clc
  67.     jmp    short get_number_6
  68. get_number_3:
  69.     cmp    al,'?'            ;did they ask for the default?
  70.     stc
  71.     jne    get_number_6        ;no, return cy.
  72.     add    si,2            ;skip past the question mark.
  73.     mov    cx,-1
  74.     mov    bx,-1
  75.     jmp    get_number_2        ;and return the -1.
  76. get_number_6:
  77.     ret
  78.